
#include "Delay.h"

#include <STC15F2K60S2.H>
//#include <AT89X51.h>
//#include <REGX52.H>

sbit COL1 = P4^4;
sbit COL2 = P4^2;
sbit COL3 = P3^5;
sbit COL4 = P3^4;

sbit ROW1 = P3^0;
sbit ROW2 = P3^1;
sbit ROW3 = P3^2;
sbit ROW4 = P3^3;

/**
  * @brief  矩阵键盘读取按键键码
  * @param  无
  * @retval KeyNumber 按下按键的键码值
			如果按键按下不放，程序会停留在此函数，松手的一瞬间，返回按键键码，没有按键按下时，返回0
  */
unsigned char MatrixKey()
{
	unsigned char KeyNumber=0;
	

	P3 = 0xFF;
	P4 = 0xFF;
//	P4 &= 0xEF; // 1110 1111
//	P4^4 = 0;
	COL1 = 0;
	if(P3==0xFE){Delay(20);while(P3==0xFE);Delay(20);KeyNumber=1;}
	if(P3==0xFD){Delay(20);while(P3==0xFD);Delay(20);KeyNumber=5;}
	if(P3==0xFB){Delay(20);while(P3==0xFB);Delay(20);KeyNumber=9;}
	if(P3==0xF7){Delay(20);while(P3==0xF7);Delay(20);KeyNumber=13;}

	P3 = 0xFF;
	P4 = 0xFF;
//	P4 &= 0xFB; // 1111 1011
//	P4^2 = 0;
	COL2 = 0;
	if(P3==0xFE){Delay(20);while(P3==0xFE);Delay(20);KeyNumber=2;}
	if(P3==0xFD){Delay(20);while(P3==0xFD);Delay(20);KeyNumber=6;}
	if(P3==0xFB){Delay(20);while(P3==0xFB);Delay(20);KeyNumber=10;}
	if(P3==0xF7){Delay(20);while(P3==0xF7);Delay(20);KeyNumber=14;}
	
	P3 = 0xFF;
	P4 = 0xFF;
//	P3 &= 0xDF; // 1101 1111
//	P3^5 = 0;
	COL3 = 0;
	if(P3==0xFE){Delay(20);while(P3==0xFE);Delay(20);KeyNumber=2;}
	if(P3==0xFD){Delay(20);while(P3==0xFD);Delay(20);KeyNumber=6;}
	if(P3==0xFB){Delay(20);while(P3==0xFB);Delay(20);KeyNumber=10;}
	if(P3==0xF7){Delay(20);while(P3==0xF7);Delay(20);KeyNumber=14;}
//	if(P3==0xFE){Delay(20);while(P3==0xFE);Delay(20);KeyNumber=3;}
//	if(P3==0xFD){Delay(20);while(P3==0xFD);Delay(20);KeyNumber=7;}
//	if(P3==0xFB){Delay(20);while(P3==0xFB);Delay(20);KeyNumber=11;}
//	if(P3==0xF7){Delay(20);while(P3==0xF7);Delay(20);KeyNumber=15;}
	

	P3 = 0xFF;
	P4 = 0xFF;
//	P3 &= 0xEF; // 1110 1111
//	P3^4 = 0;
	COL4 = 0;
	if(P3==0xFE){Delay(20);while(P3==0xFE);Delay(20);KeyNumber=4;}
	if(P3==0xFD){Delay(20);while(P3==0xFD);Delay(20);KeyNumber=8;}
	if(P3==0xFB){Delay(20);while(P3==0xFB);Delay(20);KeyNumber=12;}
	if(P3==0xF7){Delay(20);while(P3==0xF7);Delay(20);KeyNumber=16;}
	
	return KeyNumber;
}
